home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / var / lib / dpkg / info / passwd.config < prev    next >
Encoding:
Text File  |  2006-12-19  |  9.5 KB  |  351 lines

  1. #!/bin/sh -e
  2.  
  3. test -f /usr/share/debconf/confmodule || exit 0
  4.  
  5. # Check to see if the package is being reconfigured.
  6. # I don't want to bother on an initial install or on upgrade, because
  7. # some of the password stuff below could mess with a perfectly working
  8. # system when passwd was just harmlessly upgraded (that has happened
  9. # in the past).
  10. if [ "$1" != reconfigure ]; then
  11.     exit 0
  12. fi
  13.  
  14. # don't make assumptions about the umask
  15. umask 022
  16.  
  17. . /usr/share/debconf/confmodule
  18. db_capb "backup"
  19. db_settitle passwd/title
  20.  
  21. # Returns a true value if there seems to be a system user account.
  22. is_system_user () {
  23.         # Assume NIS, or any uid from 1000 to 29999,  means there is a user.
  24.         if grep -q '^+:' /etc/passwd || \
  25.            grep -q '^[^:]*:[^:]*:[1-9][0-9][0-9][0-9]:' /etc/passwd || \
  26.            grep -q '^[^:]*:[^:]*:[12][0-9][0-9][0-9][0-9]:' /etc/passwd; then
  27.                 return 0
  28.         else
  29.                 return 1
  30.         fi
  31. }
  32.  
  33. # Returns a true value if root already has a password.
  34. root_password () {
  35.     # Assume there is a root password if NIS is being used.
  36.     if grep -q '^+:' /etc/passwd; then
  37.         return 0
  38.     fi
  39.  
  40.     if [ -e /etc/shadow ] ; then
  41.         RSP="`grep ^root: /etc/shadow | cut -d : -f 2`"
  42.         [ -n "$RSP" ] && [ "x$RSP" != 'x*' ] && return 0
  43.     fi
  44.     
  45.     RPW="`grep ^root: /etc/passwd | cut -d : -f 2`"
  46.     [ -n "$RPW" ] && [ "x$RPW" != 'xx' ] && return 0
  47.  
  48.     return 1
  49. }
  50.  
  51. # Set a password, via chpasswd.
  52. # Use perl rather than echo, to avoid the password
  53. # showing in the process table. (However, this is normally
  54. # only called when first booting the system, when root has no
  55. # password at all, so that should be an unnecessary precaution).
  56. #
  57. # Pass in three arguments: the user, the password, and 'true' if the
  58. # password has been pre-crypted (by preseeding).
  59. setpassword () {
  60.     SETPASSWD_PW="$2"
  61.     export SETPASSWD_PW
  62.  
  63.     # This is very annoying. chpasswd cannot handle generating md5
  64.     # passwords as it is not PAM-aware. Thus, I have to work around
  65.     # that by crypting the password myself if md5 is used.
  66.     USE_MD5=1
  67.     export USE_MD5
  68.  
  69.         if [ "$3" = true ]; then
  70.                 PRECRYPTED=1
  71.         else
  72.                 PRECRYPTED=''
  73.         fi
  74.         export PRECRYPTED
  75.         perl -e '
  76.         sub CreateCryptSalt {
  77.             my $md5 = shift;
  78.  
  79.             my @valid = split(//, "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  80.             my ($in, $out);
  81.  
  82.             my $cryptsaltlen = ($md5 ? 8 : 2);
  83.  
  84.             open (F, "</dev/urandom") || die "No /dev/urandom found!";
  85.             foreach (1..$cryptsaltlen) {
  86.                 read(F, $in, 1);
  87.                 $out .= $valid[ord($in) % ($#valid + 1)];
  88.             }
  89.             close F;
  90.             return ($md5 ? "\$1\$$out\$" : $out);
  91.         }
  92.     
  93.         open(P,"| chpasswd -e");
  94.                 if ($ENV{PRECRYPTED}) {
  95.                         print P shift().":$ENV{SETPASSWD_PW}\n";
  96.                 } else {
  97.                         print P shift().":".
  98.                                 crypt($ENV{SETPASSWD_PW}, CreateCryptSalt($ENV{USE_MD5})).
  99.                                 "\n";
  100.                 }
  101.         close P;
  102.         ' "$1"
  103.     SETPASSWD_PW=''
  104.     USE_MD5=''
  105.         PRECRYPTED=''
  106. }
  107.  
  108. # Main loop starts here. Use a state machine to allow jumping back to
  109. # previous questions.
  110. STATE=0
  111. while [ "$STATE" != '10' ] && [ "$STATE" != '-1' ]; do
  112.     case "$STATE" in
  113.     0)
  114.         # Ask how the password files should be set up.
  115.         db_input low passwd/shadow || true
  116.     ;;
  117.     1)    
  118.     # md5 passwords are now on by default. This step is dead.
  119.     :
  120.     ;;
  121.     2)
  122.         # Enable shadowed passwords...or not
  123.         db_get passwd/shadow
  124.         if [ "$RET" = true ]; then
  125.             shadowconfig on
  126.         else
  127.             shadowconfig off
  128.         fi
  129.     ;;
  130.     3)
  131.         # Prompt for a root password if there is none.
  132.         if ! root_password; then
  133.             # First check whether the root password was preseeded crypted
  134.             db_get passwd/root-password-crypted || true
  135.             if ! test "$RET" ; then
  136.                 # No preseed of the root password hash
  137.                 # we will prompt the user
  138.                 db_input critical passwd/root-password || true
  139.                 # Note that this runs at a slightly lower
  140.                 # priority, so it may not always be seen. If
  141.                 # it isn't, don't compare passwords.
  142.                 COMPARE_PW=''
  143.                 db_input critical passwd/root-password-again \
  144.                 && COMPARE_PW=1 || true
  145.             fi
  146.         fi
  147.     ;;
  148.     4)
  149.         # Verify and set a root password.
  150.         if ! root_password; then
  151.             # First check whether the root password was preseeded crypted
  152.             db_get passwd/root-password-crypted || true
  153.             if ! test "$RET" ; then
  154.                 # Compare the two passwords, loop back if not
  155.                 # identical, or if empty.
  156.                 db_get passwd/root-password
  157.                 ROOT_PW="$RET"
  158.                 if [ -z "$ROOT_PW" ]; then
  159.                 db_fset passwd/password-empty seen false
  160.                 db_input critical passwd/password-empty
  161.                 db_fset passwd/root-password seen false
  162.                 db_fset passwd/root-password-again seen false
  163.                 STATE=2
  164.                 continue
  165.                 fi
  166.                 db_get passwd/root-password-again
  167.                 if [ "$COMPARE_PW" ] && [ "$ROOT_PW" != "$RET" ]; then
  168.                 db_fset passwd/password-mismatch seen false
  169.                 db_input critical passwd/password-mismatch
  170.                 db_fset passwd/root-password seen false
  171.                 db_fset passwd/root-password-again seen false
  172.                 STATE=2
  173.                 continue
  174.                 fi
  175.             
  176.                 # Clear root password from the db, and set the
  177.                 # password.
  178.                 db_set passwd/root-password ""
  179.                 db_set passwd/root-password-again ""
  180.                 setpassword root "$ROOT_PW" false
  181.                 ROOT_PW=''
  182.             else
  183.                 ROOT_PW="$RET"
  184.                 # The root password was preseeded encrypted
  185.                 # Clear it from the db, then set it
  186.                 db_set passwd/root-password-crypted ""
  187.                 setpassword root "$ROOT_PW" true
  188.                 ROOT_PW=''
  189.             fi
  190.             # Loop back to state #2 to make sure that there
  191.             # is a root password, and if not, prompt again.
  192.             STATE=2
  193.             continue
  194.         fi
  195.     ;;
  196.     5)
  197.         # Ask if a non-root user should be made, if there is not
  198.         # already one.
  199.         if ! is_system_user; then
  200.             db_input medium passwd/make-user || true
  201.         fi
  202.     ;;
  203.     6)
  204.         # Prompt for user info.
  205.         db_get passwd/make-user
  206.         if [ "$RET" = true ] && ! is_system_user; then
  207.             db_input critical passwd/user-fullname || true
  208.         fi
  209.     ;;
  210.     7)
  211.         # Prompt for user info.
  212.         db_get passwd/make-user
  213.         if [ "$RET" = true ] && ! is_system_user; then
  214.             LOOP=""
  215.             db_get passwd/username
  216.             if [ -z "$RET" ]; then
  217.                 db_get passwd/user-fullname
  218.                 # Login defaults to user's first name
  219.                 # Some hat off to a few d-i people
  220.                 case "$RET" in
  221.                     "Martin Michlmayr")
  222.                     userdefault="tbm"
  223.                     ;;
  224.                     *)
  225.                     userdefault=`echo $RET | sed 's/ .*//' | LC_ALL=C tr A-Z a-z`
  226.                     ;;
  227.                 esac
  228.                 if test -n "$userdefault" && \
  229.                    LC_ALL=C expr "$userdefault" : '[a-z][-a-z0-9]*$' >/dev/null; then
  230.                     db_set passwd/username "$userdefault"
  231.                 fi
  232.             fi
  233.             db_input critical passwd/username || true
  234.         fi
  235.     ;;
  236.     8)
  237.         # Verify and make user.
  238.         db_get passwd/make-user
  239.         if [ "$RET" = true ] && ! is_system_user; then
  240.             # Verify the user name, loop with message if bad.
  241.             db_get passwd/username
  242.             USER="$RET"
  243.             if ! LC_ALL=C expr "$USER" : '[a-z][-a-z0-9]*$' >/dev/null; then
  244.                 db_fset passwd/username seen false
  245.                 db_fset passwd/username-bad seen false
  246.                 db_input critical passwd/username-bad
  247.                 STATE=5
  248.                 continue
  249.             fi
  250.             
  251.             db_get passwd/user-password-crypted || true
  252.             if ! test "$RET" ; then
  253.                 db_input critical passwd/user-password || true
  254.                 COMPARE_PW=''
  255.                 db_input critical passwd/user-password-again \
  256.                 && COMPARE_PW=1 || true
  257.             fi
  258.         fi
  259.     ;;
  260.     9)
  261.         db_get passwd/make-user
  262.         if [ "$RET" = true ] && ! is_system_user; then
  263.             db_get passwd/user-password-crypted || true
  264.             if ! test "$RET" ; then
  265.                 # Compare the two passwords, loop with message if not
  266.                 # identical, or if empty.
  267.                 db_get passwd/user-password
  268.                 USER_PW="$RET"
  269.                 db_get passwd/user-password-again
  270.                 if [ "$COMPARE_PW" ] && [ "$USER_PW" != "$RET" ]; then
  271.                 db_set passwd/user-password ""
  272.                 db_set passwd/user-password-again ""
  273.                 db_fset passwd/password-mismatch seen false
  274.                 db_input critical passwd/password-mismatch
  275.                 db_fset passwd/user-password seen false
  276.                 db_fset passwd/user-password-again seen false
  277.                 STATE=8
  278.                 continue
  279.                 fi
  280.                 if [ -z "$USER_PW" ]; then
  281.                 db_set passwd/user-password ""
  282.                 db_set passwd/user-password-again ""
  283.                 db_fset passwd/password-empty seen false
  284.                 db_input critical passwd/password-empty
  285.                 db_fset passwd/user-password seen false
  286.                 db_fset passwd/user-password-again seen false
  287.                 STATE=8
  288.                 continue
  289.                 fi
  290.             else
  291.                 USER_PW=$RET
  292.             fi
  293.  
  294.             if db_get passwd/user-uid && [ "$RET" ]; then
  295.                 if test -x /usr/sbin/adduser; then
  296.                 UIDOPT="--uid $RET"
  297.                 else
  298.                 UIDOPT="-u $RET"
  299.                 fi
  300.             else
  301.                 UIDOPT=
  302.             fi
  303.  
  304.             # Add the user to the database, using adduser in
  305.             # noninteractive mode.
  306.             db_get passwd/user-fullname
  307.  
  308.             if test -x /usr/sbin/adduser; then
  309.                 adduser --disabled-password --gecos "$RET" $UIDOPT "$USER" >/dev/null || true
  310.             else
  311.                 useradd -c "$RET" -m "$USER" $UIDOPT >/dev/null || true
  312.             fi
  313.             
  314.             db_get passwd/user-password-crypted || true
  315.             if ! test "$RET" ; then
  316.                 # Clear password from the db, and set the password.
  317.                 db_set passwd/user-password ""
  318.                 db_set passwd/user-password-again ""
  319.                 db_get passwd/username
  320.                 setpassword "$USER" "$USER_PW" false
  321.                 USER_PW=''
  322.             else
  323.                 USER_PW="$RET"
  324.                 # The user password was preseeded encrypted
  325.                 # Clear it from the db, then set it
  326.                 db_set passwd/root-password-crypted ""
  327.                 setpassword "$USER" "$USER_PW" true
  328.                 USER_PW=''
  329.             fi
  330.  
  331.             # Loop back through to make sure the user was
  332.             # added.
  333.             STATE=5
  334.             continue
  335.         fi
  336.     ;;
  337.     esac
  338.  
  339.     if db_go; then
  340.         STATE=$(($STATE + 1))
  341.     else
  342.         STATE=$(($STATE - 1))
  343.     fi
  344. #    echo "ON STATE: $STATE"
  345. done
  346.  
  347. if test "$STATE" = -1 
  348. then
  349.     exit 30
  350. fi
  351.